PRG 1.0 TXT

/*
*/

//-----------------------------------------------------------------------------
// [CONFIG]

include "..\program_include.ocf"

object Program
{
	type		= e_program.DIFFUSE_SA;
	layer_bits	= e_render_layer_bits.OPAQUE;
	
	ubo_data	= array /*UBO*/ {
		{ "inMaterial",  e_program_data.MATERIAL }
	}

	textures	= array /*ProgramTexture*/ {
		{ "unDiffuseMap", e_texture.DIFFUSE },
		{ "unJointData",  e_texture.JOINTS_ARRAY },
		{ "unWeightData", e_texture.WEIGHTS_ARRAY }
	}
	
	uniforms	= array /*string*/ {
		"transform", "unTransform."
	}
}


//-----------------------------------------------------------------------------
// [VERTEX]

#version 330 core
#pragma optimize(on)

#define ATTR_POSITION	0
#define ATTR_TEXCOORD	2

layout(location = ATTR_TEXCOORD) in vec2	inTexcoord;
layout(location = ATTR_POSITION) in ivec2	inWeight;		// start index, count

uniform samplerBuffer	unJointData;			// RGBA32F : quat (4), pos (3), empty (1)
uniform samplerBuffer	unWeightData;			// RGB32F  : pos (3), joint index (1), bias (1), empty (1)


uniform struct TTransform {
	mat4	MVP;
	mat4	MV;
	mat3	Normal;
} unTransform;


out	TVertexData {
	vec2	vTexcoord;
	vec3	vNormal;
	float	fDepth;
} Output;


vec3 RotateVec(in vec4 qleft, in vec3 vright)
{
	vec3	uv,
			uuv,
			qvec = vec3(qleft);

	uv	= cross( qvec, vright );
	uuv	= cross( qvec, uv ) * 2.0;
	uv	*= 2.0 * qleft.w;
	
	return vright + uv + uuv;
}


void main()
{
	vec3	position = vec3(0.0);
	
	for (int i = 0; i < inWeight[1]; ++i)
	{
		int		idx		= ( inWeight[0] + i ) * 2;
		
		// read weight data
		vec3	w_pos	= texelFetch( unWeightData, idx + 0 ).rgb;
		vec3	w_data	= texelFetch( unWeightData, idx + 1 ).rgb;
		int		joint	= floatBitsToInt( w_data.x );
		float	bias	= w_data.y;
		
		idx = joint * 2;
		
		// read joint data
		vec4	orient	= texelFetch( unJointData, idx + 0 ).rgba;
		vec3	j_pos	= texelFetch( unJointData, idx + 1 ).rgb;
		
		position += ( j_pos + RotateVec( orient, w_pos ) ) * bias;
	}
	
	gl_Position		 = unTransform.MVP * vec4( position, 1.0 );
	Output.fDepth	 = ( unTransform.MV * vec4( position, 1.0 ) ).z;
	Output.vNormal	 = vec3(0.0, 1.0, 0.0) * unTransform.Normal;
	Output.vTexcoord = inTexcoord;
}


//-----------------------------------------------------------------------------
// [FRAGMENT]

#version 330 core
#pragma optimize(on)

layout(location = 0) out vec4	outDNNS;	// depth, normal(x,y), Shininness
layout(location = 1) out vec4	outRGBM;	// colorRGB, MaterialID

uniform sampler2D	unDiffuseMap;

layout(std140)
uniform inMaterial {
	vec3	vAmbientColor;
	vec4	vDiffuseColor;
	vec3	vSpecularColor;
	vec3	vEmissionColor;
	float	fShininess;
} unMaterial;


in	TVertexData {
	vec2	vTexcoord;
	vec3	vNormal;
	float	fDepth;
} Input;


vec2 CalcOutputNormal(vec3 vNormal)
{
	vec2	res = ( vNormal.xy + vec2(1.0) ) * 0.5;
	res.x *= ( vNormal.z < 0.0 ? -1.0 : 1.0 );
	return res;
}

void main()
{
	vec4	color = texture( unDiffuseMap, Input.vTexcoord ) * unMaterial.vDiffuseColor;

	outDNNS.r   = Input.fDepth;
	outDNNS.gb  = CalcOutputNormal( normalize( Input.vNormal ) );
	outDNNS.a	= unMaterial.fShininess;
	outRGBM.rgb	= color.rgb;
	outRGBM.a   = 0.0;
}

// [END]
